package program.stack;

import program.array.ArrayV2;

/**
 * 数组实现栈
 *  1. 使用的底层数据结构为{@link ArrayV2}.
 */
public class ArrayStack<E> implements Stack<E> {

    private ArrayV2<E> data;

    public ArrayStack(int capacity) {
        data = new ArrayV2(capacity);
    }

    public ArrayStack() {
        data = new ArrayV2<>();
    }

    /**
     * 检测栈是否为空.
     *
     * @return false:
     */
    public boolean isEmpty() {
        return data.isEmpty();
    }

    /**
     * 查看栈的容量.
     *
     * @return : 栈的容量.
     */
    public int getCapacity() {
        return data.getCapacity();
    }

    @Override
    public int getSize() {
        return data.getSize();
    }

    @Override
    public void push(E e) {
        data.addLast(e);
    }

    @Override
    public E pop() {
        return data.removeLast();
    }

    @Override
    public E peek() {
        return data.getLast();
    }

    @Override
    public String toString() {
        StringBuilder resultStr = new StringBuilder();
        int size = data.getSize();
        resultStr.append(String.format("Stack size:%d, capacity: %d\n", size, getCapacity()));
        resultStr.append("[");
        for (int i = 0; i < size; i++) {
            resultStr.append(data.get(i));
            if (i != size - 1) {
                resultStr.append(", ");
            }
        }

        resultStr.append("]top");
        return resultStr.toString();
    }
}
